package com.beamfield.controller.common;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.coobird.thumbnailator.Thumbnails;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.beamfield.common.CommonUtils;
import com.beamfield.model.FileInfo;
import com.beamfield.service.manage.sys.FileService;

/**
 *
 */
public class BaseController  {

	@Value("${session.user}")
	private String user;

	@Value("${session.uname}")
	private String uName;

	@Value("${session.beam}")
	private String beam;

	@Value("${session.beam_name}")
	private String beamName;

	@Value("${uploadFilePath}")
	private String uploadFilePath;

	@Resource
	private FileService fileService;


	protected Long getUserId(HttpServletRequest req) {
		Object obejct = req.getSession().getAttribute(user);
		return obejct == null ? null : (Long) obejct;
	}

	protected Long getBeamId(HttpServletRequest req) {
		Object obejct = req.getSession().getAttribute(beam);
		return obejct == null ? null : (Long) obejct;
	}
	protected String getBeamName(HttpServletRequest req) {
		Object obejct = req.getSession().getAttribute(beamName);
		return obejct == null ? null : (String) obejct;
	}
	protected String getUserName(HttpServletRequest req) {
		Object obejct = req.getSession().getAttribute(uName);
		return obejct == null ? null : (String) obejct;
	}

	@Transactional(readOnly = false, propagation = Propagation.SUPPORTS)
	public long saveFile(CommonsMultipartFile file) {
		int fileid = 0;
		try {
			if (file != null) {
				String myFileName = file.getOriginalFilename();
				if (!CommonUtils.isEmptyString(myFileName)) {
					String savePath = CommonUtils.getTimeFormat(new Date(), "yyyyMMdd") + File.separator;
					String path = uploadFilePath+ savePath;
					// 定义上传路径
					CommonUtils.checkPath(path);
					int index = myFileName.lastIndexOf(".");
					String extension = "";
					if (index > 0) {
						extension = myFileName.substring(index, myFileName.length());
					}
					String filename = CommonUtils.getTimeFormat(new Date(), "yyyyMMddhhmmssSSS") + "_"
							+ (int) (Math.random() * 100) + extension;
					OutputStream out = new FileOutputStream(path + myFileName);
					byte[] b = null;
					if(file.getSize()>200*1024){
						if(".jpg".equals(extension.toLowerCase())||".jpeg".equals(extension.toLowerCase())){
							Thumbnails.of(file.getInputStream()).scale(0.9f).outputQuality(0.4).toOutputStream(out);
						}
						if(".png".equals(extension.toLowerCase())){
							Thumbnails.of(file.getInputStream()).scale(0.6f).outputQuality(0.5).toOutputStream(out);
						}else {
							b = file.getBytes();
						}
					}else{
						b = file.getBytes();
					}
					if(b != null){
						out.write(b);
					}
					out.flush();
					out.close();

					// 文件表留档
					FileInfo fileInfo = new FileInfo();
					fileInfo.setAdd_time(new Date());
					fileInfo.setFile_path(savePath);
					fileInfo.setOriginal_name(myFileName);
					fileInfo.setFormer_name(myFileName);
					fileInfo = fileService.saveFileInfo(fileInfo);
					// fileid = fileInfo.getFileid();
					return fileInfo.getFileid();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fileid;
	}
}
